"use strict";
require('dotenv').config();

const express = require('express'),
      cors = require('cors'),
      path = require('path');

const app = express(),
      PORT = process.env.PORT || 5000;

const errorHandler = require('./middleware/errorHandler'),
      logger = require('./middleware/logger'),
      validator = require('./middleware/validator');

const callbackRouter = require('./routes/callbackRouter'),
      settingsRouter = require('./routes/settingsRouter'),
      membersRouter = require('./routes/membersRouter'),
      envService = require('./services/envService');

envService.update();

app
    // Middleware
    .use(cors())
    .use(express.json())
    .use(logger)
    .use(validator)

    //Static
    .use(express.static(path.resolve(__dirname, 'public')))

    //Routers
    .use('/callback', callbackRouter)
    .use('/settings', settingsRouter)
    .use('/members', membersRouter)

    //Error handler
    .use(errorHandler);

app.listen(PORT, () => console.log(`Server is listening on port ${PORT}...`));
